package com.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.mysql.jdbc.Connection;
import com.util.DB;

import edu.student.models.Friend;
import edu.student.models.Interest;

public class FriendDao {

	public static Friend findOne(String selectionModifier) {
		System.out.println("in fin One");
		//Friend selection = new Friend();
		Friend friend = new Friend();
		ResultSet resultSet = null;
		String query = "select "+"ROLL_NO, FRIEND_ROLL_NO "+
				"from friend "+ selectionModifier;
		Connection connection = (Connection) DB.getConnection();
		resultSet = DB.readFromDB(query, connection);
		try {
			if (resultSet.next()) {
				
				friend.setRollNo(resultSet.getString("ROLL_NO"));
				friend.setFriendRollNo(resultSet.getString("FRIEND_ROLL_NO"));
				//selection.add(friend);
			}
		} catch (SQLException e) {
           
			e.printStackTrace();
		}
		//DB.close(resultSet);
		System.out.println("before closing DB");
		DB.close(connection);
		System.out.println("friend roll "+friend.getRollNo());
		return friend;
	}
	public static Interest findInterest(String selectionModifier) {
		ArrayList<Interest> selection = new ArrayList<Interest>();
		ResultSet resultSet = null;
		String query = "select INTEREST_ID, INTEREST_NAME from INTEREST"+" "+ selectionModifier;
		Connection connection = (Connection) DB.getConnection();
		resultSet = DB.readFromDB(query, connection);
		Interest interest = new Interest();
		try {
			if (resultSet.next()) {
				
				interest.setInterestId(resultSet.getInt("INTEREST_ID"));
				interest.setInterestName(resultSet.getString("INTEREST_NAME"));
				}
		} catch (SQLException e) {
           // MyLog.myCatch("Book.java", 43, e);
			e.printStackTrace();
		}
		//DB.close(resultSet);
		DB.close(connection);
		return interest;
	}
	public int update(String fRollNo, String rollNo ) {
		String updateSQL = 
			    "update friend set friend_roll_no = '"+fRollNo+"' where "+"roll_no ='"+rollNo+"'";
		System.out.println(updateSQL);
		return DB.update(updateSQL);
	}

	public static int insert(String fRollNo,String rollNo) {
		String insertSQL = "insert into friend " + "values('" + rollNo
				+ "', '" + fRollNo +"')"+ ";";
		System.out.println("insert query"+insertSQL);
		return DB.update(insertSQL);
	}
}
